package com.ucw.ucwclient.biz.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ucw.ucwclient.dal.dataobject.Account;
import com.ucw.ucwclient.dal.dataobject.UStudy;
import com.ucw.ucwclient.dal.dataobject.UStudyCollect;
import com.ucw.ucwclient.dal.dataobject.UStudyEnroll;
import com.ucw.ucwclient.dal.mapper.AccountMapper;
import com.ucw.ucwclient.dal.mapper.SchoolBannerMapper;
import com.ucw.ucwclient.dal.mapper.UActivityMapper;
import com.ucw.ucwclient.dal.mapper.UStudyCollectMapper;
import com.ucw.ucwclient.dal.mapper.UStudyEnrollMapper;
import com.ucw.ucwclient.dal.mapper.UStudyMapper;
import com.ucw.ucwclient.dal.search.NormalSearch;

@Service
public class UStudyService {

	@Resource
	private UStudyMapper uStudyMapper;

	@Resource
	private UStudyCollectMapper uStudyCollectMapper;

	@Resource
	private UStudyEnrollMapper uStudyEnrollMapper;

	@Resource
	private SchoolBannerMapper schoolBannerMapper;

	@Resource
	private UActivityMapper uActivityMapper;

	@Resource
	private AccountMapper accountMapper;

	public List<UStudy> getList(NormalSearch search) {
		List<UStudy> list = uStudyMapper.selectByPageSearch(search);
		return list;
	}

	public UStudy getOne(int id) {
		return uStudyMapper.selectByPrimaryKey(id);
	}

	public Map<String, String> reportOne(Integer id) {
		Map<String, String> resp = new HashMap<String, String>();
		UStudy us = new UStudy();
		us.setId(id);
		us.setReport(true);
		int result = uStudyMapper.updateByPrimaryKey(us);
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");

		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public Map<String, String> collectOne(Integer id, Long userId) {
		Map<String, String> resp = new HashMap<String, String>();
		UStudyCollect collect = new UStudyCollect();
		collect.setCreateTime(new Date());
		collect.setStudyId(id);
		collect.setUserId(userId);
		int result = uStudyCollectMapper.insertSelective(collect);
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");
		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public Map<String, String> enrollOne(Integer id, Long userId) {
		Map<String, String> resp = new HashMap<String, String>();
		UStudyEnroll collect = new UStudyEnroll();
		collect.setCreateTime(new Date());
		collect.setStudyId(id);
		collect.setUserId(userId);
		int result = uStudyEnrollMapper.insertSelective(collect);
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");
		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public List<Account> checkOneEnroll(NormalSearch search, Long userId) {
		List<Account> list = accountMapper.selectUSEnrollByPageSearch(search);

		return list;
	}

	public Map<String, String> cancelCollectOne(Integer id, Long userId) {
		Map<String, String> resp = new HashMap<String, String>();

		int result = uStudyCollectMapper.deleteCollect(id, userId);
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");
		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public Map<String, String> cancelEnrollOne(Integer id, Long userId) {

		int result = uStudyEnrollMapper.deleteEnroll(id, userId);
		Map<String, String> resp = new HashMap<String, String>();
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");
		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public Map<String, String> addOne(UStudy us, Long userId) {
		us.setCreateTime(new Date());
		us.setUserId(userId);
		int result = uStudyMapper.insertSelective(us);
		Map<String, String> resp = new HashMap<String, String>();
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");
		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	@Transactional
	public Map<String, String> approve(List<Long> userIds, Integer enrollId, Long userId) {
		int result = 0;
		Map<String, String> resp = new HashMap<String, String>();
		if (userIds != null && userIds.size() > 0) {
			for (Long long1 : userIds) {
				UStudyEnroll ua = new UStudyEnroll();
				ua.setStudyId(enrollId);
				ua.setUserId(long1);
				ua.setEnrollAccept(true);
				result = result + uStudyEnrollMapper.updateByPrimaryKeySelective(ua);
			}
		}
		if (result > 0) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");

		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

}
